package first.niuke.simple;

/**
 * @ClassName NC110
 * @description:
 * @author: 荣燊
 * @create: 2021-06-02 14:59
 **/
public class NC110 {
    // 不允许使用另外数组
    public static void main(String[] args) {
        int n = 6;
        int m = 2;
        int[] a = {1,2,3,4,5,6};
        for (int s : a) {
            System.out.print(s);
        }
        System.out.println();
        System.out.println("————————————————————");
        for (int x : solve(n, m, a)) {
            System.out.print(x);
        }
    }

    // 不能使用其他数组，三次翻转
    public static int[] solve (int n, int m, int[] a) {
        // write code here
        if (m > n) {
            m = m%n;
        }
        if (n == 1 || m == 0) {
            return a;
        }
        // 第一次翻转，将整个数组翻转
        int length = n-1;
        for (int i=0; i<n/2; i++) {
            int tmp = a[i];
            a[i] = a[length];
            a[length] = tmp;
            --length;
        }
        // 第二次翻转，将数组的前半部分翻转
        length = m-1;
        for (int j=0; j<m/2; j++) {
            int tmp = a[j];
            a[j] = a[length];
            a[length] = tmp;
            --length;
        }
        // 第三次翻转，将数组的后半部分翻转
        length = n-1;
        for (int k=m; k<(n+m)/2; k++) {
            int tmp = a[k];
            a[k] = a[length];
            a[length] = tmp;
            length--;
        }
        return a;
    }
}
